import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {Content, Events, IonicPage, NavController, NavParams} from 'ionic-angular';
import {LoadingService} from "../../../services/common/loading.service";
import * as moment from "moment";
import {TeamMarketActivityProvider} from "../../../providers/my-team/team-market-activity/team-market-activity.provider";
import {NumberService} from "../../../services/common/number.service";
import {AdpActionSheetController} from '../../../components/common/adp-action-sheet/adp-action-sheet-controller';

@IonicPage()
@Component({
    selector: 'page-team-market-activity-jiabao-detail',
    templateUrl: 'team-market-activity-jiabao-detail.html',
    providers: [
        TeamMarketActivityProvider,
        LoadingService,
        NumberService
    ]
})
export class TeamMarketActivityJiabaoDetailPage {

    constructor(private navCtrl: NavController,
                private events: Events,
                private actionSheetCtrl: AdpActionSheetController,
                public changeRef: ChangeDetectorRef,
                private navParams: NavParams,
                public numberService: NumberService,
                private teamMarketActivityProvider: TeamMarketActivityProvider,
                private loadingService: LoadingService) {
    }

    moment = moment;
    /*筛选对象*/
    private filterParams = {
        activityID: '',
        groupID: '',
        isJoin: '',
        sort: 1
    };

    /*活动名称*/
    public name: string;
    public shortName: string;
    /*活动参与时间*/
    public activityTime: string;
    public groupList: Array<any> = [];


    /*列表数据*/
    jiaBaoDetailList: Array<any> = [];
    jiaBaoDetailTotal = {
        totalPerson: 0,
        contactVCustomerNum: 0,
        targetCustomerNum: 0,
        percentContact: '0'
    };

    /*客户跟进率 默认为降序*/
    private followRateDown: boolean = true;

    @ViewChild(Content) content: Content;
    isScrolling = false;
    isArriveBottom = false;

    ionViewDidLoad() {
        this.filterParams.activityID = this.navParams.get('id');
        this.filterParams.groupID = this.navParams.get('groupId');
        this.getJiaBaoDetailList();
    }

    /*点击客户跟进率后获取数据*/
    getJiaBaoByFollowRate() {
        this.followRateDown = !this.followRateDown;
        this.filterParams.sort = this.filterParams.sort === 1 ? 0 : 1;
        this.getJiaBaoDetailList();
    }

    showBusinessGroup(ev) {
        let temGroup = [];
        this.groupList.forEach((item) => {
            temGroup.push({
                cssClass: this.filterParams.groupID == item.groupID ? 'action-sheet-selected' : '',
                text: item.groupName,
                handler: () => {
                    this.filterParams.groupID = item.groupID;
                    this.getJiaBaoDetailList();
                }
            })
        });
        temGroup.push({
            text: '取消',
            role: 'cancel'
        });
        this.actionSheetCtrl.create({
            title: '业务组',
            cssClass: 'roster-action',
            buttons: temGroup
        }).present();
        ev.stopPropagation();
    }

    getJiaBaoDetailList() {
        this.jiaBaoDetailTotal = {
            totalPerson:0,
            contactVCustomerNum: 0,
            targetCustomerNum: 0,
            percentContact: '0'
        };
        this.loadingService.showLoading();
        this.teamMarketActivityProvider.getJiaBaoDetail(this.filterParams).subscribe((res) => {
            if (res.length == 0) {
                this.loadingService.dismissLoading();
                return;
            }
            let result = res[0];
            this.name = result.activityName;
            this.shortName = result.shortName;
            if (this.groupList.length === 0) {
                this.groupList = [{groupID: '', groupName: '全部组'}].concat(result.groups);
            }
            result.groupList.forEach((item) => {
                item.isJoin = item.isJoin === '1' ? '是' : '否';
                this.jiaBaoDetailTotal.contactVCustomerNum += parseInt(item.contactVCustomerNum || 0);
                this.jiaBaoDetailTotal.targetCustomerNum += parseInt(item.targetCustomerNum || 0);

            });

            if (this.jiaBaoDetailTotal.targetCustomerNum == 0) {
                this.jiaBaoDetailTotal.percentContact = '0%';
            } else {
                let temp1 = (this.jiaBaoDetailTotal.contactVCustomerNum / this.jiaBaoDetailTotal.targetCustomerNum).toFixed(2);
                this.jiaBaoDetailTotal.percentContact = this.numberService.multiply(temp1, 100) + '%';
            }

            let jiaBaoList=result.groupList;
            this.jiaBaoDetailTotal.totalPerson=jiaBaoList.length;
            this.jiaBaoDetailList = jiaBaoList;
            this.activityTime = moment.unix(result.starttime).format('YYYY/MM/DD') + ' - ' + this.moment.unix(result.endtime).format('YYYY/MM/DD');
            this.loadingService.dismissLoading();
        }, () => {
            this.loadingService.dismissLoading();
        });
    }

    toActivityDetail() {
        this.navCtrl.push('ActivitDetailPage', {
            id: this.filterParams.activityID,
            classify:0
        });
    }
    //滚动相关
    scrollStart() {
        this.isScrolling = true;
    }

    scrollEnd() {
        this.isScrolling = false;
    }

    scrolling() {
        const dimensions = this.content.getContentDimensions();
        const temp1 = dimensions.scrollHeight - dimensions.scrollTop - dimensions.contentHeight < 100;
        if (temp1 !== this.isArriveBottom) {
            this.isArriveBottom = temp1;
            this.changeRef.detectChanges();
        }
    }

    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }
}
